package com.xps.charminguser.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.utils.JUtils;
import com.xps.charminguser.R;
import com.xps.charminguser.bean.LoginBean;
import com.xps.charminguser.date.net.UserNetWorks;
import com.xps.charminguser.utils.SharedPreferencesUtils;
import com.xps.charminguser.view.base.BaseActivity;
import com.zhy.autolayout.AutoLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by MI on 2018/1/22.
 */

public class LoginActivity extends BaseActivity {
    @BindView(R.id.Cell_phone_number)
    EditText CellPhoneNumber;
    @BindView(R.id.Login_Password)
    EditText LoginPassword;
    // @BindView(R.id.Service_agreement)
//    CheckBox ServiceAgreement;
//    @BindView(R.id.xieye_Text)
//    TextView xieyeText;
    @BindView(R.id.Remember_password)
    CheckBox RememberPassword;
    @BindView(R.id.Sign_in)
    TextView SignIn;
    @BindView(R.id.Login_Register)
    TextView LoginRegister;
    @BindView(R.id.Forgot_Password)
    TextView ForgotPassword;
    private static final String PREFS_NAME = "MyUserInfo";
    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.actionbar_tv_title)
    TextView actionbarTvTitle;
    @BindView(R.id.home_titlecenter)
    AutoLinearLayout homeTitlecenter;
    @BindView(R.id.actionbar_tv_right)
    TextView actionbarTvRight;

    @Override
    protected int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void setData() {
        actionbarTvTitle.setText("登陆");
        actionbarTvTitle.setTextColor(getResources().getColor(R.color.white));
    }

    @Override
    protected void init(Bundle inBundle) {
        ButterKnife.bind(this);
        LoadUserDate();
    }

    @OnClick({R.id.Sign_in, R.id.Login_Register, R.id.Forgot_Password})
    public void onViewClicked(View view) {
        switch (view.getId()) {
//            case R.id.xieye_Text:
//                JUtils.Toast("正在开发");
//                break;
            case R.id.Sign_in:
                getLogin();
                SaveUserDate();
                showDialogNoCancel();
                break;
            case R.id.Login_Register:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;
            case R.id.Forgot_Password:
                JUtils.Toast("正在开发");
                break;
        }
    }

    public void getLogin() {
        UserNetWorks.UserLogin(CellPhoneNumber.getText().toString(), LoginPassword.getText().toString(), new Subscriber<LoginBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                dissmisDialog();
            }

            @Override
            public void onNext(LoginBean loginBean) {
                if (loginBean.getReturn_code().equals("SUCCESS")) {
                    dissmisDialog();
                    finish();
                    SharedPreferencesUtils.putString("user_token", "String", loginBean.getReturn_body().getRider_token());
                    Log.i("user_token", loginBean.getReturn_body().getRider_token());
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    JUtils.Toast(loginBean.getReturn_msg());
                } else if (loginBean.getReturn_code().equals("FAIL")) {
                    dissmisDialog();
                    JUtils.Toast(loginBean.getReturn_msg());
                }
            }
        });
    }

    private void SaveUserDate() {
        if (RememberPassword.isChecked()) {
            SharedPreferencesUtils.putBoolean("isSave", "boolean", true);
            SharedPreferencesUtils.putString("name", "boolean", CellPhoneNumber.getText().toString());
            SharedPreferencesUtils.putString("password", "boolean", LoginPassword.getText().toString());
        } else {
            SharedPreferencesUtils.putBoolean("isSave", "boolean", false);
            SharedPreferencesUtils.putString("name", "boolean", "");
            SharedPreferencesUtils.putString("password", "boolean", "");
        }
    }

    private void LoadUserDate() {
        if (SharedPreferencesUtils.getBoolean("isSave", "boolean", true)) {
            String username = SharedPreferencesUtils.getString("name", "boolean", "");
            String userpassword = SharedPreferencesUtils.getString("password", "boolean", "");
            if (!("".equals(username) && "".equals(userpassword))) {
                CellPhoneNumber.setText(username);
                LoginPassword.setText(userpassword);
                RememberPassword.setChecked(true);
            }
        }
    }
}
